package com.younion.action.consumer.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IFavourableEntityService;
import com.younion.common.constants.SysConstants;
import com.younion.model.FavEntsListResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.config.ConfigHelper;
import com.younion.vo.API_FavourableEntityVO;
import com.younion.vo.FavourableEntityVO;

public class FavourableEntListGetConsumer extends AbstractRequestConsumer {

	private IFavourableEntityService favourableEntityService;
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getFavourableEntityList(request, response);
	}

	public String getFavourableEntityList(HttpServletRequest request,
			HttpServletResponse response) {
		String user_token = request.getParameter("user_token");
		String card_id = request.getParameter("card_id");
		int int_page = ApiConstant.API_DEFAULT_PAGE;
		List<FavourableEntityVO> favourableEntityVOList = new ArrayList<FavourableEntityVO>();
		List<FavEntsListResult> favsListResultList = new ArrayList<FavEntsListResult>();
		Map paramMap = new HashMap();
		if(HttpRequestUtils.checkIntegerParam("card_id", card_id, response)){
		paramMap.put("cardId", Integer.valueOf(card_id));
		}
		// 发放渠道为全部或自主申请的
		paramMap.put("notreleaseChannel", 1);
		// 种类为普惠制
		paramMap.put("classification", 0);
		ResultObject resultObject = new ResultObject();
		try {
			List<API_FavourableEntityVO> apiFavEntList = (List<API_FavourableEntityVO>)favourableEntityService.getApiFavourableEntity(paramMap);
			processFavEntityData(apiFavEntList, favsListResultList,user_token);
			resultObject.setResult(favsListResultList);
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);

		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}

		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	private void processFavEntityData(
			List<API_FavourableEntityVO> apiFavEntList,
			List<FavEntsListResult> favsListResultList,
			String user_token
			) throws Exception {
		String img_url_pre = ConfigHelper.fastGetConfig("interface",
				"img_url_pre", "http://localhost:8080/younonmember/");
		if(apiFavEntList != null && apiFavEntList.size() > 0){
		for (API_FavourableEntityVO api_favourableEntity : apiFavEntList) {
			FavEntsListResult favsListResult = new FavEntsListResult();
			favsListResult.setCat_id(api_favourableEntity.getCat_id());
			favsListResult.setFav_ent_id(api_favourableEntity.getId().toString());
			favsListResult.setFav_ent_name(api_favourableEntity.getName());
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			favsListResult.setApply_date_from(
					(api_favourableEntity.getApplyBegDate()==null)?"":
					sf.format(api_favourableEntity.getApplyBegDate()));
			favsListResult.setApply_date_to(
					(api_favourableEntity.getApplyEndDate()==null)?"":
					sf.format(api_favourableEntity.getApplyEndDate()));
			favsListResult.setUse_date_from(
					(api_favourableEntity.getUseBegDate()==null)?"":
					sf.format(api_favourableEntity.getUseBegDate()));
			favsListResult.setUse_date_to(
					(api_favourableEntity.getUseEndDate()==null)?"":
					sf.format(api_favourableEntity.getUseEndDate()));
			favsListResult.setNonactivatedPic(img_url_pre+ SysConstants.FavourableEntity_IMG_URL + 
					"/"+ api_favourableEntity.getId() + "/"+api_favourableEntity.getNonactivatedPic());
			favsListResult.setActivatedPic(img_url_pre+ SysConstants.FavourableEntity_IMG_URL + 
					"/"+ api_favourableEntity.getId() + "/"+api_favourableEntity.getActivatedPic());
			favsListResult.setMoney(api_favourableEntity.getValue());
			favsListResult.setSimple_description(api_favourableEntity.getSimpleDescription());
			favsListResult.setShops(api_favourableEntity.getShops());
			favsListResult.setParticipate_num(api_favourableEntity.getParticipate_num().toString());
			//如果user_token为空，则表示没申请过
			String fav_id = "";
			if(user_token!=null && !"".equals(user_token)){
				Map map = new HashMap();
				map.put("favourableEntity",api_favourableEntity.getId());
				map.put("userToken", user_token);
				fav_id = favourableEntityService.selectAppliedNumByUserToken(map);
			}
			favsListResult.setFav_id(fav_id);
			favsListResult.setIsHot(api_favourableEntity.getIsHot().toString());
			favsListResult.setIsRecommend(api_favourableEntity.getIsRecommend().toString());
			favsListResult.setIsNew(api_favourableEntity.getIsNew().toString());
			favsListResult.setIsAuthenByMoblie(api_favourableEntity.getIsAuthenByMoblie());
			favsListResultList.add(favsListResult);
		}
	}
	}

	public void setFavourableEntityService(
			IFavourableEntityService favourableEntityService) {
		this.favourableEntityService = favourableEntityService;
	}

}
